﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyFirstWindowsFormsApplication
{

    public partial class PrinterUserControl : UserControl 
    {
        public event EventHandler PageMissing;
        public event EventHandler InkEmpty ;
        private static int printnum ;
        //get printer name
        public string PrinterName {
            get{return printerNameLabel.Name;}
            set{printerNameLabel.Name = value;}
        }
        public int InkCount {
            get{return inkCountProgressBar.Value;}
            set{
                inkCountProgressBar.Value = value;
                PrinterUserControl_InkEmpty();
            }
        }
        public int PageCount {
            get{return (int)pageCountNnumericUpDown.Value;}
            set { 
                pageCountNnumericUpDown.Value = value;
               // PrinterUserControl_pageMissing(value);
            }
        }
        public PrinterUserControl()
        {
            InitializeComponent();

            
            PrinterName = "Printer" +(printnum+++1).ToString();
            printerNameLabel.Text = PrinterName;
        }
        /// <summary>
        /// fanction to event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       private void PrinterUserControl_InkEmpty()
        {
          
            //low problem
            if (this.InkCount < 15 && this.InkCount >10)
            {
                this.inkLabel.ForeColor = System.Drawing.Color.Yellow;

                if (InkEmpty != null) InkEmpty(this, new PrinterEventArgs { Critical = false, Date = DateTime.Now, msg = "Your ink is only" + (this.InkCount).ToString() + "%" });
            }
            //meduim problem
            if (this.InkCount < 10 && this.InkCount > 1)
            {
                this.inkLabel.ForeColor = System.Drawing.Color.Orange;
                //calling to event
                if (InkEmpty != null) InkEmpty(this, new PrinterEventArgs { Critical = false, Date = DateTime.Now, msg = "Your ink is only" + (this.InkCount).ToString() + "%" });
                                          
            }
            if (this.InkCount < 1)
            {
                this.inkLabel.ForeColor = System.Drawing.Color.Red;
                PrinterEventArgs er = new PrinterEventArgs();
                if (InkEmpty != null) InkEmpty(this, new PrinterEventArgs { Critical = true, Date = DateTime.Now, msg = "Your ink is only" + (this.InkCount).ToString() + "%" });

                addInk();
            }
        }

       public void PrinterUserControl_pageMissing(int pageCount)
       {
           this.printerNameLabel.ForeColor = System.Drawing.Color.Red;
           //PrinterEventArgs er = new PrinterEventArgs { Critical = true, Date = DateTime.Now, msg = "Missing" + (this.PageCount - pageCount).ToString() + "pages" };

           if (PageMissing!=null)
           {
               PageMissing(this, new PrinterEventArgs { Critical = true, Date = DateTime.Now, msg = "Missing" + ( pageCount) + "pages" }); 
           }
                    addPage();
       }
 

        public void print()
        {
            Random rand = new Random();
            PrinterUserControl_InkEmpty();
            
            InkCount -= rand.Next(10, 30);
            int page = rand.Next(15, 30);
            PrinterUserControl_pageMissing(page);
            PageCount -= page;
            /* 
            InkCount -= ink;
            PageCount -= page;
            */
        }
        public void  addInk()
        {
            Random rand = new Random();
            InkCount = rand.Next(50, 100);

        }
        public void addPage()
        {
            Random rand = new Random();
            PageCount = rand.Next(100, 400);

        }

        private void PrinterUserControl_Load(object sender, EventArgs e)
        {

        }

        private void ptinterPictureBox_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// when mouse over on name 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printerNameLabel_MouseHover(object sender, EventArgs e)
        {
            this.printerNameLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177))); 
        }
        /// <summary>
        /// when mouse leave the name 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printerNameLabel_MouseLeave(object sender, EventArgs e)
        {
            this.printerNameLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177))); 

        }



    }
}
